/**
 * 
 */
package boundary.components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFileChooser;

import executor.QuadTree;

/**
 * @author davide
 *
 */
public class QuadTreePreview extends JComponent implements PropertyChangeListener
{
    private static final long serialVersionUID = 4732723881616703807L;
    
    File file = null;
    ImageIcon thumbnail = null;
    
    public QuadTreePreview(JFileChooser fc)
    {
	this.setPreferredSize(new Dimension(100, 50));
	fc.addPropertyChangeListener(this);
    }
    
    public void loadImage()
    {
        if (this.file == null) {
            this.thumbnail = null;
            return;
        }
        
        ImageIcon tmpIcon = this.createImageIcon(this.file.getAbsoluteFile());
        
        if (tmpIcon != null)
        {
            if (tmpIcon.getIconWidth() > 90)
            {
                this.thumbnail = new ImageIcon(tmpIcon.getImage().
                                          getScaledInstance(90, -1, Image.SCALE_FAST));
            }
            
            else {
                this.thumbnail = tmpIcon;
            }
        }
    }

    public void propertyChange(PropertyChangeEvent e)
    {
        boolean update = false;
        String prop = e.getPropertyName();

        if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
            this.file = null;
            update = true;
        }
        
        // If a file became selected, find out which one.
        else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
            this.file = (File) e.getNewValue();
            update = true;
        }

        //Update the preview accordingly.
        if (update) {
            this.thumbnail = null;
            if (isShowing()) {
                loadImage();
                repaint();
            }
        }
    }
    
    @Override
    protected void paintComponent(Graphics g)
    {
        if (this.thumbnail == null) {
            loadImage();
        }
        if (this.thumbnail != null) {
            int x = getWidth()/2 - this.thumbnail.getIconWidth()/2;
            int y = getHeight()/2 - this.thumbnail.getIconHeight()/2;

            if (y < 0) {
                y = 0;
            }

            if (x < 5) {
                x = 5;
            }
            this.thumbnail.paintIcon(this, g, x, y);
        }
    }
    
    private ImageIcon createImageIcon(File f)
    {
	QuadTree qTree;
	ImageIcon image;
	
	try {
            qTree = QuadTree.read(new FileInputStream(f));
            
            qTree.prepareImage(Color.black);
            image = new ImageIcon(qTree.getImage());
        }
        
        catch(Exception ex) {
            image = null;
        }
        
        return image;
    }
}
